/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.chat.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.chat.handler
 * @author:LiBo/Alex
 * @create-date:2020-07-28 9:47
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: 初始化相关的channel通道对象操作服务处理
 */
public class WebSocketChanneInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * 配置以及初始化相关的channel通道服务控制器
     * @param socketChannel
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //请求解码器
        socketChannel.pipeline().addLast("http-codec", new HttpServerCodec());
        //将多个消息转换成单一的消息对象
        socketChannel.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        //支持异步发送大的码流，一般用于发送文件流
        socketChannel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        //处理 websocket 和处理消息的发送
        socketChannel.pipeline().addLast("handler", new WebSocketSimpleChannelInboundHandler());
    }
}
